package org.example.enums;

import java.util.Arrays;
import java.util.Objects;

public enum ErrorCodeEnum {
    SUCCESS(1000, "成功"),
    SERVICE_EXCEPTION(1001, "服务内部异常"),
    SPEL_EVAL_EXCEPTION(1002, "spel表达式({})有误"),
    PARAMS_EXCEPTION(1003, "参数校验失败"),
    DISTRIBUTE_LOCK_NAME_NOT_NULL_EXCEPTION(1004, "分布式锁名称不能为空"),
    DISTRIBUTE_LOCK_BUSINESS_EXCEPTION(1005, "业务操作异常"),
    DISTRIBUTE_LOCK_FAIL_EXCEPTION(1006, "加锁失败"),
    KAFKA_MSG_SEND_EXCEPTION(1007, "kafka消息发送失败"),
    REDIS_EXE_EXCEPTION(1008, "redis命令执行异常"),
    ;

    private Integer code;

    private String desc;

    public Integer getCode() {
        return code;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    ErrorCodeEnum(Integer code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    public static ErrorCodeEnum getByCode(Integer code) {
        if (null == code) {
            return null;
        }
        return Arrays.stream(values()).filter(errorCodeEnum -> Objects.equals(errorCodeEnum.code, code)).findFirst().orElse(null);
    }
}
